package com.qfedu.mavenk.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc: Weapon主类
 * @datatime: 2022/10/19 20:03
 */
public class App {  //角色
    Weapon weapon;

    public App(){
        this.weapon = new Sword();
    }

    //bean依赖注入(构造注入)
    public App(Weapon weapon){
        this.weapon = weapon;
    }

    public void attack(){
        this.weapon.attack();
    }

    public static void main(String[] args) {
        //读取配置文件    ClassPathXmlApplicationContext加载spring的xml配置文件
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
        //通过bean唯一标识id值来获取xml配置文件中的数据注入,也就是已经注册实例化的bean
        App app = (App) classPathXmlApplicationContext.getBean("app");
        app.attack();
        ((ClassPathXmlApplicationContext)classPathXmlApplicationContext).close();
    }
}
